package cm.word.generate.web;

import cm.word.generate.dao.mysql.MysqlQueryMapper;
import cm.word.generate.pojo.mysql.MysqlTableDto;
import cm.word.generate.pojo.mysql.MysqlWordDto;
import cm.word.generate.utils.JacobOperaword;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.File;
import java.util.List;

/**
 * @author caomin
 * @date 2018/5/7
 * @说明 word
 */
@RequestMapping("/mysql")
public class MysqlWordController {


    @Autowired
    MysqlQueryMapper mysqlQueryMapper;

    @GetMapping
    public void test(String dataBaseName) throws Exception {
        JacobOperaword jacobOperaword = new JacobOperaword(true);
        //创建文档
        jacobOperaword.createNewDocument();

        if (StringUtils.isEmpty(dataBaseName)) {
            dataBaseName = "xmall";
        }
        String targetFilePath = "d:/mysqlGen.doc";
        File file = new File(targetFilePath);
        if (file.getParentFile() == null) {
            file.mkdirs();
        }

        List<MysqlTableDto> tables = mysqlQueryMapper.getTables(dataBaseName);
        for (int i = 0; i < tables.size(); i++) {
            MysqlTableDto mysqlTableDto = tables.get(i);
            String tableName = mysqlTableDto.getTableName();
            String tableComment = mysqlTableDto.getTableComment();
            List<MysqlWordDto> wordList = mysqlQueryMapper.getColumnList(tableName);
            int tableIndex = i + 1;
            int cols = 6, rows = wordList.size() + 1;
            String text = tableName + "表详细设计";
            jacobOperaword.insertText(tableName + "表详细设计");
            jacobOperaword.moveDown(2);
            jacobOperaword.moveRight(2);
            jacobOperaword.createTable(text, cols, rows);

            jacobOperaword.putTxtToCell(tableIndex, 1, 1, "代码");
            jacobOperaword.putTxtToCell(tableIndex, 1, 2, "名称");
            jacobOperaword.putTxtToCell(tableIndex, 1, 3, "数据类型");
            jacobOperaword.putTxtToCell(tableIndex, 1, 4, "是否为空");
            jacobOperaword.putTxtToCell(tableIndex, 1, 5, "主键");
            jacobOperaword.putTxtToCell(tableIndex, 1, 6, "备注");
            for (int j = 0; j < wordList.size(); j++) {
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 1, wordList.get(j).getColumnName());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 2, wordList.get(j).getBz());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 3, wordList.get(j).getAllColumnType());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 4, wordList.get(j).getSfwk());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 5, wordList.get(j).getColumnKey());
                jacobOperaword.putTxtToCell(tableIndex, j + 2, 6, wordList.get(j).getExtra());
            }
            jacobOperaword.moveDown(3);
            jacobOperaword.insertText("我是华丽的分隔线");
        }
        jacobOperaword.save(targetFilePath);
        jacobOperaword.setSaveOnExit(true);
        jacobOperaword.closeDocumentWithoutSave();
        jacobOperaword.closeDocument();
        jacobOperaword.close();


//        File file = new File(path);
//        ServletOutputStream outputStream = null;
//        try {
//            httpServletResponse.reset();//清空输出流
//            httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName + ".kml", "UTF-8"));
//            httpServletResponse.addHeader("Content-Length", "" + file.length());
//            outputStream = httpServletResponse.getOutputStream();
//            byte[] bytes = FileUtils.getBytes(file);
//            outputStream.write(bytes);
//            outputStream.flush();
//        } catch (Exception e) {
//            throw new BusinessException("下载失败");
//        } finally {
//            try {
//                if (outputStream != null) {
//                    outputStream.close();//关闭输出流
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        }


    }

}
